

function say() {
    alert("LC:57. 插入区间;抄的答案 https://leetcode-cn.com/problems/insert-interval/solution/shou-hua-tu-jie-57-cha-ru-qu-jian-fen-cheng-3ge-ji/");
}
say();

/**
 * @param {number[][]} intervals
 * @param {number[]} newInterval
 * @return {number[][]}
 */
var insert = function(intervals, newInterval) {
    //9:37-52    10:05 - 23   26?
    // 11:00-11:15
    //左 中 右
    var res = []
    var idx = 0
    var len = intervals.length

    while(idx < len && intervals[idx][1] < newInterval[0]){
        res.push(intervals[idx])
        idx ++
    }
    
    while(idx < len && intervals[idx][0] <= newInterval[1]) {
        newInterval[0] = Math.min(intervals[idx][0], newInterval[0])
        newInterval[1] = Math.max(intervals[idx][1],newInterval[1])
        idx++
    }
    
    res.push(newInterval)

    while(idx < len){
        res.push(intervals[idx])
        idx++
    }
    return res
};



function temp (){

//找到new元素在int的下标
 if(intervals.length == 0){
        return [newInterval]
    }

    var firstE = newInterval[0]
    var secE = newInterval[1]
    var idxF = -1
    var idxS = -1

    if(intervals.length == 1){
        if(firstE < intervals[0][0] && secE > intervals[0][1]){
            intervals[0][0] = firstE
            intervals[0][1] = secE
            return intervals
        }
    }

    for(var i = 0; i < intervals.length; i ++){
        var element = intervals[i]
        if(firstE >= element[0] && firstE <= element[1] ){
            idxF = i
        }

        if(secE >=element[0] && secE <= element[1]){
            idxS = i
        }
    }

    if(idxF >= 0 && idxS < 0){
        intervals[idxF][1] = secE
        return intervals
    }else if(idxF >= 0 && idxS >= 0){
        if(idxF == idxS){
            return intervals
        }else {
            // var target =[ intervals[idxF][0] , intervals[idxS][1]]
            intervals[idxF][1] = intervals[idxS][1]
            intervals.splice(idxF + 1, (idxS - idxF))
            return intervals
        }
    }else if(idxF < 0 && idxS >= 0){
        console.log('dddd')
        intervals[0][0] = firstE
        return intervals
    }
    
    else {
        console.log('???',idxF,'??',idxS)
        //没有交叉 找到目标位置,遍历
        if(intervals[0][0] > firstE && intervals[intervals.length - 1][1] < secE){
            intervals.length = 0
            intervals.push(newInterval)
            return intervals
        }

        if(intervals[0][0] > secE){
            console.log('从头插入')
            intervals.unshift(newInterval)
        }else if(intervals[intervals.length - 1][1] < firstE){
            console.log('从尾插入')
            intervals.push(newInterval)
        }else { //从中间插入
            var targetIdx = 0
            for(var i = 0; i < intervals.length ; i++){
                var current = intervals[i]
                var next = intervals[i + 1]
                if(current[1] < firstE && next[1] > firstE){
                    targetIdx = i
                    break
                }
            }
            console.log('下标',targetIdx)
            intervals.splice(targetIdx + 1,0,newInterval)
        }

        return intervals
    }
}
